package com.fish.yssb.ui.page.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.ui.page.activity.wave.CtrlActivity;
import com.fish.yssb.ui.page.util.TopToastUtil;

import java.io.File;
import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class ctrlAdapter extends RecyclerView.Adapter<ctrlAdapter.BaseViewHolder> {
    private ArrayList<BaseInfoTable> dataList = new ArrayList<BaseInfoTable>();
    private Resources res;
    private Context m_context;
    private CtrlActivity ctrlActivity;
    public void replaceAll(ArrayList<BaseInfoTable> list, CtrlActivity activity) {
        ctrlActivity = activity;
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }


    @Override
    public ctrlAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        m_context =parent.getContext();
        return new ctrlAdapter.realtimeYCViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.ctrl_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ctrlAdapter.BaseViewHolder holder, int position) {

        holder.setData(dataList.get(position));
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(BaseInfoTable data) {
        }
    }

    private class realtimeYCViewHolder extends ctrlAdapter.BaseViewHolder {


        private TextView tv_name;
        private TextView tv_size;
        private TextView tv_o_c;
        private TextView tv_op,tv_op2;
        private CheckBox ck_o_c;
        public realtimeYCViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_size = view.findViewById(R.id.tv_size);
            tv_o_c = view.findViewById(R.id.tv_o_c);
            tv_op = view.findViewById(R.id.tv_op);
            tv_op2 = view.findViewById(R.id.tv_op2);
            ck_o_c = view.findViewById(R.id.ck_o_c);
        }

        @Override
        void setData(final BaseInfoTable data) {
            if (data != null) {
                tv_name.setText(data.getName());
                tv_size.setText(data.getConnAdr());

//                tv_time.setText(data.getFileDate());
//                tv_unit.setText(data.getUnit());

                tv_op.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

//                        TopToastUtil.getInstance((Activity) m_context,R.layout.showdialog).showShortToast("预制成功");

                        Boolean flag = ck_o_c.isChecked();
                        short num = StringToInt(data.getConnAdr());
                        ctrlActivity.setData(num,flag);

                    }
                });

                tv_op2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Boolean flag = ck_o_c.isChecked();
                        short num = StringToInt(data.getConnAdr());
                        ctrlActivity.setData_1(num,flag);
//                        TopToastUtil.getInstance((Activity) m_context,R.layout.showdialog).showShortToast("执行成功");

                    }
                });

            }



        }
    }


    private short StringToInt(String str)
    {
        if(str.length() ==6) {

            String tmp = str.substring(4,6)+str.substring(2,4);
            Log.e("=========tmp:",tmp);
            short num = Short.parseShort(tmp,16);
            return num;
        }
        return 0;
    }

}